package insta.project;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class PlaylistsGroup extends ActivityGroup {

	public static PlaylistsGroup group;
	private static final int ID_MENU = 0;
	private static final int NEW_MENU_ITEM = 1;
	private static final int REMOVE_MENU_ITEM = 2;
	private static final int VIEW_MENU_ITEM = 3;
	private ArrayList<View> history;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<View>();
		group = this;

		Intent intent = new Intent().setClass(this, PlayListActivity.class);
		intent.putExtra("action", "view");
		View view = getLocalActivityManager().startActivity("PlaylistActivity",
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

		replaceView(view);
	}

	public void replaceView(View v) {
		history.add(v);
		setContentView(v);
	}

	public void back() {
		if (history.size() > 0) {
			Log.d("playlistgroup_history_size", "" + history.size());
			history.remove(history.size() - 1);
			if (history.size() - 1 > -1) {
				setContentView((View) history.get(history.size() - 1));
			} else {
				finish();
			}
		} else {
			finish();
		}
	}

	public boolean onContextItemSelected(MenuItem item) {
		View view = null;
		Intent intent;
		switch (item.getItemId()) {
		case NEW_MENU_ITEM:
			intent = new Intent().setClass(this, NewPlayListActivity.class);
			view = getLocalActivityManager().startActivity("NewPlaylistActivity",
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			break;
		case REMOVE_MENU_ITEM:
			intent = new Intent().setClass(this, PlayListActivity.class);
			intent.putExtra("action", "remove");
			view = getLocalActivityManager().startActivity("PlaylistActivity",
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			break;
		case VIEW_MENU_ITEM:
			intent = new Intent().setClass(this, PlayListActivity.class);
			intent.putExtra("action", "view");
			view = getLocalActivityManager().startActivity("PlaylistActivity",
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
			break;
		}
		
		if (view != null) replaceView(view);
		return super.onContextItemSelected(item);
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Playlist Menu");
		menu.add(ID_MENU, NEW_MENU_ITEM, Menu.NONE, R.string.menu1);
		menu.add(ID_MENU, REMOVE_MENU_ITEM, Menu.NONE, R.string.menu2);
		menu.add(ID_MENU, VIEW_MENU_ITEM, Menu.NONE, R.string.menu3);
	}

	@Override
	public void onBackPressed() {
		PlaylistsGroup.group.back();
		return;
	}
}